﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Kugar.Core.ExtMethod;
using Kugar.UI.Web.AdminLTE.Helpers;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace Kugar.UI.Web.AdminLTE.PageControls.PageForms
{
    public class PageForm_FileUploadField:PageForm_SingleInputFieldBase
    {
        private string _filter = "*";
        private int _fileSize = 0;
        private string _uploadUrl = "";
        private string _srcUrl = "";
        private bool _disabled = false;
        private string _customUpload = "";

        public PageForm_FileUploadField(PageForm_Form form, ViewContext viewContext) : base(form, viewContext)
        {
            this.Css("webui-form_singlefileuploadfield singlefileuploader");
        }

        /// <summary>
        /// 自动上传的服务器地址(未配置该地址的情况下,未form提交时,上传,,)
        /// </summary>
        /// <param name="uploadUrl">服务器地址</param>
        /// <returns></returns>
        public PageForm_FileUploadField UploadUrl(string uploadUrl)
        {
            _uploadUrl = uploadUrl;
            return this;
        }

        /// <summary>
        /// 自定义上传功能
        /// </summary>
        /// <param name="afterSelectCallback">选择文件后,调用该函数,参数为 (File file)</param>
        /// <returns></returns>
        public PageForm_FileUploadField CustomUpload(string afterSelectCallback)
        {
            _customUpload = afterSelectCallback;

            return this;
        }
        /// <summary>
        /// 原始值
        /// </summary>
        /// <param name="fileUrl"></param>
        /// <returns></returns>
        public PageForm_FileUploadField Src(string fileUrl)
        {
            _srcUrl = fileUrl;
            return this;
        }

        public PageForm_FileUploadField Disabled(bool disabled = true)
        {
            _disabled = disabled;

            return this;
        }

        /// <summary>
        /// 文件类型过滤器
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        public PageForm_FileUploadField ExtensionFilter(string filter)
        {
            _filter = filter;

            return this;
        }

        /// <summary>
        /// 可上传的最大大小,单位为KB
        /// </summary>
        /// <param name="size"></param>
        /// <returns></returns>
        public PageForm_FileUploadField MaxFileSize(int size)
        {
            _fileSize = size*1024;

            return this;
        }

        protected override void BeforeRender(TextWriter writer)
        {
            this.Data("uploadurl", _uploadUrl)
                .Data("name", _name)
                .Data("accept", _filter)
                .Data("maxfilesize", _fileSize.ToStringEx())
                .Data("disabled", _disabled.ToStringEx())
                .Data("customupload",_customUpload)
                ;

            base.BeforeRender(writer);
        }


        protected override void RenderContent(TextWriter writer)
        {
            //writer.WriteLine($"<label class=\"form-control filepath\">{_srcUrl}</label>");
            writer.WriteLine($"<input type=\"text\" class=\"form-control filepath\" value=\"{_srcUrl}\" readonly/>");

            //if (!string.IsNullOrWhiteSpace(_srcUrl))
            //{
                writer.WriteLine($"<input type=\"hidden\" value=\"{_srcUrl}\" name=\"{_name}\" />");
            //}

            //writer.WriteLine($"<input type=\"file\" accept=\"{_filter}\" data-maxfilesize=\"{_fileSize}\" data-uploadurl=\"{_uploadUrl}\" data-autoupload=\"{!string.IsNullOrWhiteSpace(_uploadUrl)}\" name=\"{_name}\"/><input type=\"hidden\" value=\"{_srcUrl}\" name=\"{_name}\" />");

            writer.WriteLine($"<span class=\"input-group-btn uploader\"><button type=\"button\" class=\"btn clearfile\"  {_disabled.If("disabled")} ><i class=\"fa fa-close\"></i></button><button type=\"button\"  {_disabled.If("disabled")} class=\"btn btn-info uploaderbtn\"><i class=\"fa fa-upload\"></i></button></span>");

        }
    }
}
